# coding=utf-8
from flask import current_app
from qiniu import Auth, put_data

access_key = 'uzc59bVURbUbazey9vrexXKocNKBUN8NuLijk57N'
secret_key = '-9lenw28jU2REojvGkcsEPWk5Nm9V2HIVqb5Nkts'

# 要上传的空间
bucket_name = 'ihome-sz08'


def storage(data):
    if not data:
        return None
    try:
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 生成上传Token,可以指定过期时间等
        token = q.upload_token(bucket_name)
        # 上传文件
        ret, info = put_data(token, None, data)
        print ret, info
    except Exception as e:
        current_app.logger.error(e)
        raise e
    if info and info.status_code != 200:
        raise Exception("上传文件到七牛失败")
    # 返回七牛中保存的图片名,这个图片也是访问七牛获取图片的路径
    return ret["key"]

if __name__ == '__main__':
    file_name = raw_input("输入上传的文件:")
    with open(file_name, "rb") as f:
        storage(f.read())